<?php
// +----------------------------------------------------------------------
// | 自定义公共函数文件
// +----------------------------------------------------------------------


/**
 * 通过省CODE显示省份
 * @return province_name
 */
function showProvince($pid){
    return M('province')->where(array('province_id'=>$pid))->getField('province_name');
}

function getProvince(){
    return M('province')->field('province_id,province_name')->select();
}

/**
 * 通过CODE显示市名称
 * @return city_name
 */
function showCity($cid){
    return M('city')->where(array('city_id'=>$cid))->getField('city_name');
}
/**
 * 通过CODE显示市名称
 * @return city_name
 */
function showArea($aid){
    return M('area')->where(array('area_id' => $aid))->getField('area_name');
}

/**
 * 状态是否审核
 * @param boolean (0,1)
 * @return 是/否
 */
function showStatus($status){
    return ($status==true)?'<font color="#ff0000">是</font>':'<font color="#008000">否</font>';
}

/**
 * 返回通过状态
 * @param $status
 * @return string
 */
function showPass($status){
    if($status==0){
        $re='<font color="#008000">审核中</font>';
    }elseif($status==1){
        $re='<font color="#ff0000">通过</font>';
    }elseif($status==2){
        $re='<font color="#ff5500">未通过</font>';
    }elseif($status==5){
        $re='<font color="#85df75">草稿</font>';
    }
    return $re;
}


/**
 * 是否自己会员
 */
function showIfus($ifus){
    return ($ifus==true)?'<font color="#ff0000">[自]</font>':'';
}

/**
 * 烘焙师认证
 * @param $status
 * @return string
 */
function showIfrz($status){
    if($status==0){
        $re='<font color="#008000">未认证</font>';
    }elseif($status==1){
        $re='<font color="#ff0000">认证</font>';
    }elseif($status==2){
        $re='<font color="#000000">未通过</font>';
    }elseif($status==3){
        $re='<font color="blue">已申请</font>';
    }
    return $re;
}

function showDs($status)
{
    if($status==0){
        $re='<font color="#008000">未申请</font>';
    }elseif($status == 1){
        $re='<font color="#ff0000">通过</font>';
    }elseif($status== -1){
        $re='<font color="#000000">未通过</font>';
    }elseif($status==2){
        $re='<font color="blue">待审核</font>';
    }
    return $re;
}

/**
 * 通过ID获取用户名
 * @param $userid
 * @return mixed
 */
function getUserName($userid){
    if($userid==0){
        return '<font color="red">管理员</font>';
    }else{
        return M('User')->where(array('userid'=>$userid))->getField('uname');
    }
}

/**
 * 获取产品类别名称
 * @param $itemid
 * @return mixed
 */
function getCateName($cateid){
    return M('Category')->where(array('cateid'=>$cateid))->getField('catename');
}

/**
 * 通过ID返回名称
 * @param $id
 * @return mixed
 */
function getShipway($id){
    return M('Shipment')->where(array('id'=>$id))->getField('shipname');
}

/**
 * 获取交换意向
 * @param $mindid
 * @return string
 */
function getchangemind($mindid){

    $string='';
    $where['parentid']=array('NEQ',0);
    $arr=M('Category')->field('cateid,catename')->where($where)->select();
    $mindid=explode(',',$mindid);
    foreach($arr as $value){
        if(in_array($value['cateid'],$mindid)){
            $string.="<input type='checkbox' value=".$value['cateid']." name='mindid[]' checked='checked'>".$value['catename']."&nbsp";
        }else{
            $string.="<input type='checkbox' name='mindid[]' value=".$value['cateid'].">".$value['catename']."&nbsp";
        }
    }
    return $string;
}

/**
 * 根据ID获取新闻栏目
 * @param $id
 * @return mixed
 */
function getNewsCate($id){
    return M('NewsCate')->where(array('id'=>$id))->getField("title");
}


/**
 * 获取栏目
 * @param $id
 * @return mixed
 */
function getAdertMenu($data){
    $arr=array( '1'=>'首页',
                '2'=>'动态',
                '3'=>'头条',
                '4'=>'随手拍',
                '5'=>'积分换礼',
                '6'=>'免费试用',
                '7'=>'优品汇',
                '8'=>'作品详情页',
                '9'=>'个人中心',
                '10' => 'wap首页'
    );
    foreach($arr as $k=>$v){
        if($data==$k){
            $menu=$v;
        }
    }
    return $menu;
}

/**
 * 获取广告位置
 * @param $data
 * @return string
 */
function getAdertPosition($data){
    if($data==1){
        $pos='中间';
    }elseif($data==2){
        $pos='左侧';
    }elseif($data==3){
        $pos='右侧';
    }
    return $pos;
}

/**
 * 获取会员头像
 * @param $str
 * @return string
 */
function getUavatar($str){
  
    if(empty($str)){
        $uavatar=WEB_URL.'/static/home/images/default_touxiang.jpg';
    }else{
       $se = substr($str,0,7);
       if($se == 'http://'){
             $uavatar=$str;
       }else{
           $uavatar=IMAGES_URL.$str;
       }
       
    }
    return $uavatar;
}

/**
 * 获取USERID
 * @param $openuid
 * @return mixed
 */
function getUidByOpenuid($openuid){
    $userid=M('User')->where(array('openuid'=>$openuid))->getField('userid');
    if($userid){
        return $userid;
    }else{
        return false;
    }
}

/**
 * 返回性别
 * @param $str
 * @return string
 */
function getUsex($str){
    if($str==1){
        $usex='男';
    }elseif($str==2){
        $usex='女';
    }else{
        $usex='未知';
    }
    return $usex;
}

/**
 * 返回统计
 * @param $table
 * @param $where
 * @return mixed
 */
function getCount($table,$where){
    $count=M($table)->where($where)->count();
    return $count;
}

/**
 * 返回为空的值并赋值为0
 * @param $str
 * @return int
 */
function getNullNum($str){
    if($str==null){
        $str=0;
    }
    return $str;
}

/**
 * 返回烘焙还是随手拍作品详情
 * @param $type
 * @return string
 */
function getWorkUrl($type){
    if($type==1){
        $url='works';
    }else{
        $url='copy';
    }
    return $url;
}

/**
 * 返回试用商家姓名
 * @param $str
 * @return mixed
 */
function showTryMember($str){
    return M('TryMember')->where(array('id'=>$str))->getField('mname');
}

/**
 * 获取产品类别名称
 * @param $itemid
 * @return mixed
 */
function getTryCateName($cateid){
    return M('TryCategory')->where(array('cateid'=>$cateid))->getField('catename');
}

/**
 * 获取用户昵称
 * @param $uid
 * @return mixed
 */
function getNickName($uid){
    return M('User')->where(array('userid'=>$uid))->getField('nickname');
}

/**
 * 获取试用产品名称
 * @param $tryid
 * @return mixed
 */
function getTryItemName($tryid){
    return M('TryItems')->where(array('tryid'=>$tryid))->getField('title');
}

/**
 * 获取礼品名称
 * @param $giftid
 * @return mixed
 */
function getGiftsName($giftid){
    return M('Gifts')->where(array('id'=>$giftid))->getField('title');
}

/**
 * 更新留言数
 * @param $table
 * @param $where
 * @param $opt
 * @return mixed
 */
function optMsgnum($table,$where,$opt){
    if($opt=='add'){
        $data['msgnum'] = array('exp','msgnum+1');
    }else{
        $data['msgnum'] = array('exp','msgnum-1');
    }
    $result=M($table)->where($where)->save($data);
    return $result;
}

/**
 * 返回留言链接
 * @param $type
 * @param $id
 * @param $other
 */

function getMsgLink($type,$id,$other){
    switch ($type){
        case '1':
            $link='works/'.$id;
            break;
        case '2':
            $link='u/'.$other.'/message';
            break;
        case '3':
            $link='try/reportdetail/'.$id.'.html';
            break;
        case '4':
            $link='suipai/'.$id;
            break;
        case '5':
            $link='news/'.$id.'.html';
            break;
        case '6':
            $link='activity/'.$id.'.html';
            break;
        case '7':
            $link='teach/'.$id;
            break;
    }
    return $link;
}


/**
 * 获取留言对应的表
 * @param $type
 * @return string
 */
function getMsgTable($type){
    switch ($type){
        case '1':
            $table='Works';
            break;
        case '2':
            $table='User';
            break;
        case '3':
            $table='TryReport';
            break;
        case '4':
            $table='Suipai';
            break;
        case '5':
            $table='News';
            break;
        case '6':
            $table='Activity';
            break;
        case '7':
            $table="Teachlist";
            break;
    }
    return $table;
}

/**
 * 获取留言对应的表
 * @param $type
 * @return string
 */
function getMsgType($type){
    switch ($type){
        case '1':
            $str='烘焙作品';
            break;
        case '2':
            $str='会员';
            break;
        case '3':
            $str='试用报告';
            break;
        case '4':
            $str='随拍作品';
            break;
        case '5':
            $str='新闻';
            break;
        case '6':
            $str='活动';
            break;
        case '7':
            $str='导师专栏';
            break;
    }
    return $str;
}

/**
 * 获取留言对应的标题
 * @param unknown $type
 * @param unknown $id
 * @return string
 */

function getMsgTitle($type, $id)
{
	switch ($type){
        case '1':
            $table='Works';
        	$map['workid'] = $id;
            break;
        case '2':
            $table='User';
        	$map['userid'] = $id;
            break;
        case '3':
            $table='TryReport';
        	$map['id'] = $id;
            break;
        case '4':
            $table='Suipai';
        	$map['id'] = $id;
            break;
        case '5':
            $table='News';
        	$map['id'] = $id;
            break;
        case '6':
            $table='Activity';
        	$map['id'] = $id;
            break;
        case '7':
            $table='Teachlist';
            $map['teachid'] = $id;
            break;
    }
    if($type == 2)
    {
    	$title = M($table)->where($map)->getField("nickname");
    	$title .= "的空间留言";
    }
    else 
    {
    	$title = M($table)->where($map)->getField("title");
    	$title = hy_substr($title, 30, true);
    }
    return $title;
}

function getPageUrl($type, $infoid, $id)
{
	$url = "/";
	switch ($type){
		case '1':
			$table='Works';
			$map['workid'] = $infoid;
			$url .= "works/".$infoid;
			break;
		case '2':
			$table='User';
			$map['userid'] = $infoid;
			$openid = M($table)->where($map)->getField('openuid');
			$url .= "u/".$openid."/message";
			break;
		case '3':
			$table='TryReport';
			$map['id'] = $infoid;
			$url .= "try/reportdetail/".$infoid.".html";
			break;
		case '4':
			$table='Suipai';
			$map['id'] = $infoid;
			$url .= "suipai/".$infoid;
			break;
		case '5':
			$table='News';
			$map['id'] = $infoid;
			$url .= "news/".$infoid.".html";
			break;
		case '6':
			$table='Activity';
			$map['id'] = $infoid;
			$url .= "activity/".$infoid.".html";
			break;
		case '7':
		    $table='Teachlist';
		    $map['teachid'] = $infoid;
		    $url .= "teach/".$infoid;
		    break;
	}
	$parent_id = M("UserMessage")->where(array('id' => $id))->getField('parent_id');
	$yf_id = $id;
	if($parent_id != 0)
	{
		$yf_id = $parent_id;
	}
	$info = M("UserMessage")->where(array('infoid' => $infoid, 'parent_id' => 0, 'type' => $type))->order("create_time desc")->field("id")->select();
	$ids = array_map('array_shift', $info);
	$count = array_keys($ids, $yf_id, true);
	$page = ceil(($count[0] + 1) / C('YF_PAGE'));
	$url .= "?page=".$page."#send_".$yf_id;
	return $url;
}


/**
 * 插入系统消息
 * @param $userid
 * @param $title
 * @param $content
 * @return bool
 */
function insertSysmsg($userid,$title,$content){
//	echo $userid;exit;
    $data['type']=2;
    $data['userid']=$userid;
    $data['title']=$title;
    $data['content']=$content;
    $data['create_time']=time();
    $data['ifread']=false;
    $data['status']=true;
    $info['sysid'] = M('Sysmsg')->data($data)->add();
    $info['userid'] = $userid;
    $info['ifread'] = 0;
    M('SysmsgLog')->data($info)->add();
}

function hy_strlen($str,$char='utf8')
{
	$real_num=0;
	$num = strlen($str);
	for($i=0;$i<$num;$i++)
	{
	 
	if(ord(substr($str,$i,$i+1)) > 127)
	{


	if($char=='utf8')
		{
			$i = $i+2;//utf8下为加2
	}
	else//gbk
				{
				$i = $i+1;
			}

			}
			$real_num++;
	}
			return $real_num;
			}

function hy_substr($str,$num,$ellipsis=false)
{
	$new_str='';
	$str=trim($str);
	$suffix='';
	if($ellipsis)
	{
		$suffix='...';
	}
	if(hy_strlen($str)<=$num)
	{
		$new_str=$str;
	}
	else
	{
		for($i=0;$i<$num;$i++)
		{
			if(ord(substr($str,$i,$i+1)) > 127)
			{
				$new_str.=substr($str,$i,3);
				$i = $i+2;
				$num = $num+2;
			}
			else
			{
				$new_str.=substr($str,$i,1);
			}
		}
		$new_str=$new_str.$suffix;
	}
	return $new_str;
}
